package pers.sen.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;
import pers.sen.health.date.DateUtils;
import pers.sen.health.general.EmptyUtil;
import pers.sen.health.mapper.OrderSettingMapper;
import pers.sen.health.po.OrderSetting;
import pers.sen.health.service.OrderSettingService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author Jason
 * @version 1.0.0
 * @date 2020/11/9 20:22
 * @Description TODO
 */
@Service
@Transactional
public class OrderSettingServiceImpl extends ServiceImpl<OrderSettingMapper, OrderSetting> implements OrderSettingService {
    // 将读取的List<String[]>转成List<OrderSetting>，用来在数据库中存储数据
    private List<OrderSetting> parseExcelString(List<String[]> excel) {
        List<OrderSetting> orderSettings = new ArrayList<>();
        if (!EmptyUtil.isNullOrEmpty(excel)) {
            for (String[] strings : excel) {
                if (!EmptyUtil.isNullOrEmpty(strings)) {
                    OrderSetting orderSetting = new OrderSetting();
                    orderSetting.setOrderDate(DateUtils.parseString2Date(strings[0], "yyyy/MM/dd"));
                    orderSetting.setNumber(Integer.parseInt(strings[1]));
                    orderSettings.add(orderSetting);
                }
            }
            return orderSettings;
        }
        return null;
    }

    // 在数据库中添加数据
    @Override
    public void importOrderSettings(List<String[]> excel) {

        List<OrderSetting> orderSettings = parseExcelString(excel);

        // 根据日期查询：日期存在批量更新（设置主键），日期不存在批量添加（并且已经预约人数置为0）
        for (OrderSetting orderSetting : orderSettings) {
            if (!EmptyUtil.isNullOrEmpty(orderSetting)) {
                // 按照日期查询是否存在
                QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("ORDERDATE", orderSetting.getOrderDate());
                OrderSetting orderSettingByExcel = baseMapper.selectOne(queryWrapper);
                if (!EmptyUtil.isNullOrEmpty(orderSettingByExcel)) {
                    // 数据库查到对象
                    orderSetting.setId(orderSettingByExcel.getId());
                    orderSetting.setReservations(orderSettingByExcel.getReservations());
                } else {
                    // 不存在，添加：已经预约人数为0
                    orderSetting.setReservations(0);
                }
            }
        }
        // Batch批量添加/更新，提高效率
        saveOrUpdateBatch(orderSettings);

    }

    // 查询当月数据
    @Override
    public Map<String, Object> findSettingData(int year, int month) {

        String queryString = year + "-%" + month + "-" + "%";
        List<OrderSetting> orderSettings = baseMapper.findSettingDataByYearAndMonth(queryString);
        Map<String, Object> map = new HashMap<>();
        for (OrderSetting orderSetting : orderSettings) {
            String dateString = DateUtils.parseDate2String(orderSetting.getOrderDate(), "yyyy-MM-dd");
            Map orderSettingMap = new HashMap();
            orderSettingMap.put("number", orderSetting.getNumber());
            orderSettingMap.put("reservations", orderSetting.getReservations());
            map.put(dateString, orderSettingMap);
        }
        return map;
    }

    @Override
    public Boolean updateNumberBySettingDay(int number, String settingDay) {
        return baseMapper.updateNumberByOrderDate(number, settingDay);
    }

    @Override
    @LcnTransaction
    public int isOrderOk(String orderdate) {

        return baseMapper.isOrderOk(orderdate);
    }

    @Override
    @LcnTransaction
    public void updateReservationByOrderDate(String orderdate) {
        baseMapper.updateReservationByOrderDate(orderdate);
    }


}
